﻿
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
#endregion

namespace LongNet.Web.Email.MIME
{
    /// <summary>
    /// Header of the MIME message
    /// </summary>
    public class MIMEHeader
    {
        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public MIMEHeader()
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="HeaderText">Text for the header</param>
        public MIMEHeader(string HeaderText)
        {
            if(string.IsNullOrEmpty(HeaderText))
                throw new ArgumentNullException("Header text can not be null");
            StringReader Reader=new StringReader(HeaderText);
            try
            {
                string LineRead=Reader.ReadLine();
                string Field=LineRead+"\r\n";
                while(!string.IsNullOrEmpty(LineRead))
                {
                    LineRead=Reader.ReadLine();
                    if (!string.IsNullOrEmpty(LineRead) && (LineRead[0].Equals(' ') || LineRead[0].Equals('\t')))
                    {
                        Field += LineRead + "\r\n";
                    }
                    else
                    {
                        Fields.Add(new Field(Field));
                        Field = LineRead + "\r\n";
                    }
                }
            }
            finally
            {
                Reader.Close();
                Reader=null;
            }
        }
        #endregion

        #region Public Properties
        private List<Field> _Fields=new List<Field>();
        /// <summary>
        /// The individual fields for the header
        /// </summary>
        public List<Field>Fields
        {
            get{return _Fields;}
            set{_Fields=value;}
        }

        /// <summary>
        /// Can be used to get a specific field based on its name
        /// </summary>
        /// <param name="Key">Name of the field</param>
        /// <returns>Field specified</returns>
        public Field this[string Key]
        {
            get
            {
                foreach (Field TempField in Fields)
                {
                    if (TempField.Name.Equals(Key,StringComparison.InvariantCultureIgnoreCase))
                    {
                        return TempField;
                    }
                }
                return null;
            }
        }
        #endregion
    }
}